package com.wyl.home.timetable

import android.view.View
import androidx.databinding.ObservableArrayList
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.toDisposables
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.ObservableBody
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.ACacheHelper
import com.wyl.base.HOME_HOT_PLAYER
import com.wyl.base.bean.VideoData
import com.wyl.base.openVideoDetailPage
import com.wyl.home.R
import com.wyl.home.databinding.HotPlayActivityBinding
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.toast

class HotPlayActivity : BindingActivity<HotPlayActivityBinding>() {
    val videoList = ObservableArrayList<VideoData>()

    override fun getLayoutId(): Int = R.layout.hot_play_activity

    override fun initView() {
        binding.vm = this
        binding.mainInclude.root.setOnClickListener(this)
        binding.mainInclude2.root.setOnClickListener(this)

        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }
    }

    override fun loadData() {
        OkGo.post<BaseResponse<List<VideoData>>>(HOME_HOT_PLAYER)
            .params("appUserId", ACacheHelper.id)
            .converter(object : JsonConvert<BaseResponse<List<VideoData>>>() {})
            .adapt(ObservableBody<BaseResponse<List<VideoData>>>())
            .doOnSubscribe { showLoadingDialog() }
            .doFinally { dissmissLoadingDialog() }
            .map { it.data }
            .subscribe(
                {
                    videoList.addAll(it)
                }, {
                    toast(onError(it))
                    it.printStackTrace()
                }
            ).toDisposables(disposables)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.main_include -> openVideoDetailPage(videoList[0].appFolkOperaId)
            R.id.main_include2 -> openVideoDetailPage(videoList[1].appFolkOperaId)
        }
        return
    }


}
